package com.oocl.File;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;

public class Test2 {
	@Test
	public void test1() throws Exception{
		OutputStream out=new FileOutputStream("data.txt");
		byte[] buf=new byte[26];
		for(int i=97;i<123;i++){
			buf[i-97]=(byte)i;
		}
		out.write(buf);
		out.close();
	}
	@Test
	public void test2() throws Exception{
	
		InputStream in=new FileInputStream("data.txt");
		int m=in.read();
		m=in.read();
		in.skip(1);
		m=in.read();
		System.out.println((char)m);
		in.close();
	}
	@Test
	public void test3() throws Exception{
		byte[] buf=new byte[26];
		InputStream in=new FileInputStream("data.txt");
		int count=in.read(buf,0,5);
		System.out.println("count:"+count);
		for(int i=0;i<count;i++){
			System.out.println((char)buf[i]);
		}
		in.close();
	}
	
	@Test
	public void test4() throws Exception{
		InputStream in=new FileInputStream("data.txt");
		int m=0;
		while((m=in.read())!=-1)
			System.out.println((char)m);
		in.close();
	}
	
	@Test
	public void test5() throws Exception{
		InputStream in=new FileInputStream("data.txt");
		byte[] buf=new byte[in.available()];
		in.read(buf);
		String s=new String(buf,"UTF-8");
		System.out.println(s);
		in.close();
	}
	@Test
	public void test6() throws Exception{
		InputStream in=new FileInputStream("data.txt");
		byte[] buf=new byte[6];
		int count=0;
		while((count=in.read(buf))!=-1){
			for(int i=0;i<count;i++){
				System.out.println((char)buf[i]);
			}
		}
	}
	
	@Test
	public void test7() throws Exception{
		OutputStream out=new FileOutputStream("data1.txt");
		String s="东方海外";
		out.write(s.getBytes("gbk"));
		out.close();
	}
	
	@Test
	public void test8() throws Exception{
		InputStream in=new FileInputStream("data1.txt");
		byte[] buf=new byte[in.available()];
		in.read(buf);
		String s=new String(buf,"gbk");
		System.out.println(s);
		in.close();
	}
	
	@Test
	public void test9() throws Exception{
		//byte[] buf=new byte[1024];
		OutputStream out=new ByteArrayOutputStream(1024);
		out.write("abcd".getBytes());
		out.close();
		ByteArrayOutputStream out1=(ByteArrayOutputStream)out;
		byte[] buf=out1.toByteArray();
		//System.out.println(buf[0]);
		
		InputStream in=new ByteArrayInputStream(buf);
		int m=in.read();
		System.out.println(m);
		in.close();
		
	}
	
	public static void main(String[] args) throws Exception{
		

	}

}
